<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="签到方式" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择签到方式" clearable>
          <el-option
              v-for="dict in dict.type.gigi_sign_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="签到状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option
              v-for="dict in dict.type.gigi_sign_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发布教师" prop="teacher">
        <el-select
            v-model="queryParams.userId"
            placeholder="请选择教师"
            clearable
        >
          <el-option v-for="item of teachers" :key="item.id" :value="item.id" :label="item.name"/>
        </el-select>
      </el-form-item>
      <el-form-item label="课程(课程码)" prop="teacher">
        <el-select
            v-model="queryParams.courseId"
            placeholder="请选择课程"
            clearable
        >
          <el-option v-for="item of courses" :key="item.id" :value="item.id" :label="item.name + '(' + item.no + ')'"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="signList" @selection-change="handleSelectionChange">
      <el-table-column label="发布教师" align="center" prop="userName"/>
      <el-table-column label="教师工号" align="center" prop="userNo"/>
      <el-table-column label="课程(课程码)" align="center" prop="courseName"/>
      <el-table-column label="班级" align="center" prop="className"/>
      <el-table-column label="签到方式" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gigi_sign_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="口令" align="center" prop="pwd">
        <template slot-scope="scope">
          {{ scope.row.pwd === 0 ? null : scope.row.pwd }}
        </template>
      </el-table-column>
      <el-table-column label="经纬度" align="center" prop="location"/>
      <el-table-column label="范围(米)" align="center" prop="locationRange"/>
      <el-table-column label="开始时间" align="center" prop="startTime" />
      <el-table-column label="结束时间" align="center" prop="endTime" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gigi_sign_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleRecord(scope.row.id)"
              v-hasPermi="['gigi.sign:sign:edit']"
          >查看考勤
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['gigi.sign:sign:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改考勤管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="签到方式" prop="type">
          <el-select v-model="form.type" placeholder="请选择签到方式">
            <el-option
                v-for="dict in dict.type.gigi_sign_type"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程id" prop="courseId">
          <el-input v-model="form.courseId" placeholder="请输入课程id"/>
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id"/>
        </el-form-item>
        <el-form-item label="口令" prop="pwd">
          <el-input v-model="form.pwd" placeholder="请输入口令"/>
        </el-form-item>
        <el-form-item label="经纬度" prop="location">
          <el-input v-model="form.location" placeholder="请输入经纬度"/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
                          v-model="form.startTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
                          v-model="form.endTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="范围" prop="locationRange">
          <el-input v-model="form.locationRange" placeholder="请输入范围"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSign, getSign, delSign, addSign, updateSign} from "@/api/gigi.sign/sign";
import {listUser} from "@/api/gigi.user/user";
import {listCourse} from "@/api/gigi.course/course";

export default {
  name: "Sign",
  dicts: ['gigi_sign_type', 'gigi_sign_state'],
  data() {
    return {
      courses: [],
      teachers: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤管理表格数据
      signList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        state: null,
        locationRange: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          {required: true, message: "签到方式不能为空", trigger: "change"}
        ],
        courseId: [
          {required: true, message: "课程id不能为空", trigger: "blur"}
        ],
        userId: [
          {required: true, message: "用户id不能为空", trigger: "blur"}
        ],
        pwd: [
          {required: true, message: "口令不能为空", trigger: "blur"}
        ],
        state: [
          {required: true, message: "状态不能为空", trigger: "blur"}
        ],
        locationRange: [
          {required: true, message: "范围不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getTeachers();
    this.getCourses();
  },
  methods: {
    handleRecord(id){
      this.$router.push({path: "/check-attendance/record", query: {signInId: id}});
    },
    getCourses() {
      listCourse({
            pageNum: 1,
            pageSize: 100,
            name: null,
            no: null,
            pic: null,
            classId: null,
            userId: null
          }
      ).then(response => {
        this.courses = response.rows;
      });
    },
    getTeachers() {
      listUser({
            pageNum: 1,
            pageSize: 100,
            type: 0,
            no: null,
            name: null,
            phone: null,
            password: null,
            pic: null
          }
      ).then(response => {
        this.teachers = response.rows;
      });
    },
    /** 查询考勤管理列表 */
    getList() {
      this.loading = true;
      listSign(this.queryParams).then(response => {
        this.signList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        courseId: null,
        userId: null,
        pwd: null,
        location: null,
        state: null,
        startTime: null,
        endTime: null,
        locationRange: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考勤管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSign(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考勤管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSign(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSign(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除考勤管理编号为"' + ids + '"的数据项？').then(function () {
        return delSign(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gigi.sign/sign/export', {
        ...this.queryParams
      }, `sign_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
